package com.jnxg.smartcommunity.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.jnxg.smartcommunity.R;
import com.jnxg.smartcommunity.fragment.AssociationFragment;
import com.jnxg.smartcommunity.fragment.FriendFragment;
import com.jnxg.smartcommunity.fragment.HomeFragment;
import com.jnxg.smartcommunity.fragment.MyFragment;
import com.jnxg.smartcommunity.fragment.ServerFragment;
import com.jnxg.smartcommunity.listener.NavListener;
import com.jnxg.smartcommunity.utils.NavUtils;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by Administrator on 2017/2/28 0028.
 */

public class MainActivity extends FragmentActivity implements NavListener, View.OnClickListener {

    private static final String CURR_INDEX = "currIndex";
    private static int currIndex = 0;
    private RadioGroup group;
    private ArrayList<String> fragmentTags;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar 这是顶部的沉浸式
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        // Translucent navigation bar 这是底部的沉浸式
////            window.setFlags(
////                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
////                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
//        NavUtils.setRightBtn("点我", Color.GRAY,this,this);
        fragmentManager = getSupportFragmentManager();
        initData(savedInstanceState);
        initView();
        //   NavUtils.setLeftBtn(R.mipmap.icon_back_white, this, this);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(CURR_INDEX, currIndex);
    }

    private void initData(Bundle savedInstanceState) {
        fragmentTags = new ArrayList<String>(Arrays.asList("HomeFragment", "ServerFragment", "AssociationFragment", "FriendFragment", "MyFragment"));
        currIndex = 0;
        if (savedInstanceState != null) {
            currIndex = savedInstanceState.getInt(CURR_INDEX);
            hideSavedFragment();
        }
    }

    private void hideSavedFragment() {
        Fragment fragment = fragmentManager.findFragmentByTag(fragmentTags.get(currIndex));
        if (fragment != null) {
            fragmentManager.beginTransaction().hide(fragment).commit();
        }
    }

    private void initView() {
        NavUtils.setTitle("智慧社区", Color.WHITE, MainActivity.this);
        group = (RadioGroup) findViewById(R.id.group);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_home:
                        currIndex = 0;
                        NavUtils.setTitle("智慧社区", Color.WHITE, MainActivity.this);
                        break;
                    case R.id.rb_server:
                        currIndex = 1;
                        NavUtils.setTitle("管家", Color.WHITE, MainActivity.this);
                        break;
                    case R.id.rb_association:
                        currIndex = 2;
                        NavUtils.setTitle("协会", Color.WHITE, MainActivity.this);
                        break;
                    case R.id.rb_friend:
                        currIndex = 3;
                        NavUtils.setTitle("政务", Color.WHITE, MainActivity.this);
                        break;
                    case R.id.rb_my:
                        currIndex = 4;
                        NavUtils.setTitle("我的", Color.WHITE, MainActivity.this);
                        break;
                    default:
                        break;
                }
                showFragment();
            }
        });
        showFragment();
    }

    private void showFragment() {

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment = fragmentManager.findFragmentByTag(fragmentTags.get(currIndex));
        if (fragment == null) {
            fragment = instantFragment(currIndex);
        }
        for (int i = 0; i < fragmentTags.size(); i++) {
            Fragment f = fragmentManager.findFragmentByTag(fragmentTags.get(i));
            if (f != null && f.isAdded()) {
                fragmentTransaction.hide(f);
            }
        }
        if (fragment.isAdded()) {
            fragmentTransaction.show(fragment);
        } else {
            fragmentTransaction.add(R.id.fl_main_content, fragment, fragmentTags.get(currIndex));
        }
        fragmentTransaction.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }

    private Fragment instantFragment(int currIndex) {
        switch (currIndex) {
            case 0:
                return new HomeFragment();
            case 1:
                return new ServerFragment();
            case 2:
                return new AssociationFragment();
            case 3:
                return new FriendFragment();
            case 4:
                return new MyFragment();
            default:
                return null;
        }
    }

    private long time;

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - time > 1.5 * 1000L) {
            Toast.makeText(this, "在按一次退出", Toast.LENGTH_SHORT).show();
            time = System.currentTimeMillis();
            return;
        }
        moveTaskToBack(true);

        super.onBackPressed();
    }

    @Override
    public void onClickLeft() {

    }

    @Override
    public void onClickRight() {

    }

    @Override
    public void onClick(View view) {

    }
}
